﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace _4_6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initHScrollBar();
            initPicture();
        }

        private void initHScrollBar()
        {
            trackBar1.Minimum = 0;
            trackBar1.Maximum = 119;
            trackBar1.SmallChange = 5;
            trackBar1.LargeChange = 20;

            trackBar1.Value = 100;

            trackBar1.Dock = DockStyle.Bottom;
        }

        private void initPicture()
        {
            pictureBox1.Image = Image.FromFile("flower.jpg");
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            zoomPic(
            trackBar1.Value);
        }

        private void zoomPic(int ratio)
        {
            float ratio_f = (float)ratio / 100;
            pictureBox1.Width = (int)(this.ClientSize.Width * ratio_f);
            pictureBox1.Height = (int)((this.ClientSize.Height -
            trackBar1.Height) * ratio_f);
            pictureBox1.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
            pictureBox1.Top = (this.ClientSize.Height -
            trackBar1.Height - pictureBox1.Height) / 2;
            this.Text = "图像浏览器 " + ratio.ToString() + "%";
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            zoomPic(
            trackBar1.Value);
        }
    }
}